﻿using kad2SimSoft.Classes;
using kad2SimSoft.Resources;
using kad2SimSoft.Resources.Lokalisierung;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace kad2SimSoft
{
    /// <summary>
    /// Interaktionslogik für WarehouseControl.xaml
    /// </summary>
    public partial class WarehouseControl : UserControl
    {
        private double purchasedPartsSummary = 0.0, manufcaturedPartsSummary = 0.0, endproductsSummary = 0.0, warehouseValue = 0.0;
        private double purchasedPartsMax = 0.0, manufacturedPartsMax = 0.0, endproductsMax = 0.0;
        private ObservableCollection<Point> _purchasedPartsDataPoints = new ObservableCollection<Point>();
        private ObservableCollection<Point> _manufacturedPartsDataPoints = new ObservableCollection<Point>();
        private ObservableCollection<Point> _endproductsDataPoints = new ObservableCollection<Point>();
        private List<PurchasedPart> _purchasedParts = new List<PurchasedPart>();
        private List<ManufacturedPart> _manufacturedParts = new List<ManufacturedPart>();
        private List<Endproduct> _endproducts = new List<Endproduct>();
        private string chartHeader = null;
        //private DataTable products = null;
        public ICollectionView Products { get; private set; }
        public ICollectionView GroupedProducts { get; private set; }
        private string _image;

        public string Image
        {
            get { return _image; }
            set { _image = value; }
        }

        public ICollectionView GroupedProductlist { get; private set; }

        public WarehouseControl(List<PurchasedPart> purchasedParts, List<ManufacturedPart> manufacturedParts, List<Endproduct> endproducts)
        {
            InitializeComponent();
            DataContext = this;
            _purchasedParts = purchasedParts;
            _manufacturedParts = manufacturedParts;
            _endproducts = endproducts;
            Init(purchasedParts, manufacturedParts, endproducts);
            rbStockValue.IsChecked = true;
            chartHeader = kad2SimSoft.Properties.Resources.dgWarehouseColHeaderStockValue;
        }

        private void Init(List<PurchasedPart> purchasedParts, List<ManufacturedPart> manufacturedParts, List<Endproduct> endproducts)
        {
            collectColumnDataPoints(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderStockValue);
            calculateColumnChart();
            fillOverviewSection();
            InitializeDataGridView();
        }

        private void collectColumnDataPoints(string DisplayType)
        {
            _purchasedPartsDataPoints.Clear();
            _manufacturedPartsDataPoints.Clear();
            _endproductsDataPoints.Clear();
            purchasedPartsMax = 0;
            manufacturedPartsMax = 0;
            endproductsMax = 0;
            purchasedPartsSummary = 0.0;
            manufcaturedPartsSummary = 0.0;
            endproductsSummary = 0.0;
            warehouseValue = 0.0;

            foreach (PurchasedPart purchasedPart in _purchasedParts)
            {
                if (DisplayType.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderStockValue))
                {
                    _purchasedPartsDataPoints.Add(new Point(purchasedPart.Type + purchasedPart.Id.ToString(), purchasedPart.Price * purchasedPart.Amount));
                }
                else if (DisplayType.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderAmount))
                {
                    _purchasedPartsDataPoints.Add(new Point(purchasedPart.Type + purchasedPart.Id.ToString(), purchasedPart.Amount));
                }
                else if (DisplayType.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderPrice))
                {
                    _purchasedPartsDataPoints.Add(new Point(purchasedPart.Type + purchasedPart.Id.ToString(), purchasedPart.Price));
                }
            }

            foreach (ManufacturedPart manufacturedPart in _manufacturedParts)
            {
                if (DisplayType.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderStockValue))
                {
                    _manufacturedPartsDataPoints.Add(new Point(manufacturedPart.Type + manufacturedPart.Id.ToString(), manufacturedPart.Price * manufacturedPart.Amount));
                }
                else if (DisplayType.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderAmount))
                {
                    _manufacturedPartsDataPoints.Add(new Point(manufacturedPart.Type + manufacturedPart.Id.ToString(), manufacturedPart.Amount));
                }
                else if (DisplayType.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderPrice))
                {
                    _manufacturedPartsDataPoints.Add(new Point(manufacturedPart.Type + manufacturedPart.Id.ToString(), manufacturedPart.Price));
                }
            }

            foreach (Endproduct endproduct in _endproducts)
            {
                if (DisplayType.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderStockValue))
                {
                    _endproductsDataPoints.Add(new Point(endproduct.Type + endproduct.Id.ToString(), endproduct.Price * endproduct.Amount));
                }
                else if (DisplayType.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderAmount))
                {
                    _endproductsDataPoints.Add(new Point(endproduct.Type + endproduct.Id.ToString(), endproduct.Amount));
                }
                else if (DisplayType.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderPrice))
                {
                    _endproductsDataPoints.Add(new Point(endproduct.Type + endproduct.Id.ToString(), endproduct.Price));
                }
            }

            foreach (Point p in _purchasedPartsDataPoints)
            {
                if (purchasedPartsMax < p.Value)
                {
                    purchasedPartsMax = p.Value;
                }
                purchasedPartsSummary += p.Value;
            }

            foreach (Point p in _manufacturedPartsDataPoints)
            {
                if (manufacturedPartsMax < p.Value)
                {
                    manufacturedPartsMax = p.Value;
                }
                manufcaturedPartsSummary += p.Value;
            }

            foreach (Point p in _endproductsDataPoints)
            {
                if (endproductsMax < p.Value)
                {
                    endproductsMax = p.Value;
                }
                endproductsSummary += p.Value;
            }
            warehouseValue = purchasedPartsSummary + manufcaturedPartsSummary + endproductsSummary;
        }

        private void InitializeDataGridView()
        {
            var _products = new List<DisplayObjectProduct>();

            foreach (Endproduct eproduct in _endproducts)
            {

                if ((eproduct.Amount * 100 / eproduct.StartAmount) < 25)
                {
                    Image = @"Resources\Images\dark_red.png";
                }
                else if ((eproduct.Amount * 100 / eproduct.StartAmount) >= 25 && (eproduct.Amount * 100 / eproduct.StartAmount) < 50)
                {
                    Image = @"Resources\Images\red.png";
                }
                else if ((eproduct.Amount * 100 / eproduct.StartAmount) >= 50 && (eproduct.Amount * 100 / eproduct.StartAmount) < 80)
                {
                    Image = @"Resources\Images\yellow.png";
                }
                else if ((eproduct.Amount * 100 / eproduct.StartAmount) >= 80 && (eproduct.Amount * 100 / eproduct.StartAmount) < 100)
                {
                    Image = @"Resources\Images\green.png";
                }
                else
                {
                    Image = @"Resources\Images\dark_green.png";
                }
                _products.Add(new DisplayObjectProduct
                {
                    Id = eproduct.Type + eproduct.Id,
                    Description = eproduct.Description,
                    Type = kad2SimSoft.Properties.Resources.dgWarehouseRowValueEndproduct,
                    Amount = eproduct.Amount,
                    StartAmount = eproduct.StartAmount,
                    Ratio = eproduct.Amount * 100 / eproduct.StartAmount,
                    ImageSource = Image,
                    Price = eproduct.Price,
                    StockValue = eproduct.Amount * eproduct.Price
                });
            }

            foreach (ManufacturedPart manufacturedPart in _manufacturedParts)
            {

                if ((manufacturedPart.Amount * 100 / manufacturedPart.StartAmount) < 25)
                {
                    Image = @"Resources\Images\dark_red.png";
                }
                else if ((manufacturedPart.Amount * 100 / manufacturedPart.StartAmount) >= 25 && (manufacturedPart.Amount * 100 / manufacturedPart.StartAmount) < 50)
                {
                    Image = @"Resources\Images\red.png";
                }
                else if ((manufacturedPart.Amount * 100 / manufacturedPart.StartAmount) >= 50 && (manufacturedPart.Amount * 100 / manufacturedPart.StartAmount) < 80)
                {
                    Image = @"Resources\Images\yellow.png";
                }
                else if ((manufacturedPart.Amount * 100 / manufacturedPart.StartAmount) >= 80 && (manufacturedPart.Amount * 100 / manufacturedPart.StartAmount) < 100)
                {
                    Image = @"Resources\Images\green.png";
                }
                else
                {
                    Image = @"Resources\Images\dark_green.png";
                }
                _products.Add(new DisplayObjectProduct
                {
                    Id = manufacturedPart.Type + manufacturedPart.Id,
                    Description = manufacturedPart.Description,
                    Type = kad2SimSoft.Properties.Resources.dgWarehouseRowValueManufactured,
                    Amount = manufacturedPart.Amount,
                    StartAmount = manufacturedPart.StartAmount,
                    Ratio = manufacturedPart.Amount * 100 / manufacturedPart.StartAmount,
                    ImageSource = Image,
                    Price = manufacturedPart.Price,
                    StockValue = manufacturedPart.Amount * manufacturedPart.Price
                });
            }

            foreach (PurchasedPart purchasedPart in _purchasedParts)
            {

                if ((purchasedPart.Amount * 100 / purchasedPart.StartAmount) < 25)
                {
                    Image = @"Resources\Images\dark_red.png";
                }
                else if ((purchasedPart.Amount * 100 / purchasedPart.StartAmount) >= 25 && (purchasedPart.Amount * 100 / purchasedPart.StartAmount) < 50)
                {
                    Image = @"Resources\Images\red.png";
                }
                else if ((purchasedPart.Amount * 100 / purchasedPart.StartAmount) >= 50 && (purchasedPart.Amount * 100 / purchasedPart.StartAmount) < 80)
                {
                    Image = @"Resources\Images\yellow.png";
                }
                else if ((purchasedPart.Amount * 100 / purchasedPart.StartAmount) >= 80 && (purchasedPart.Amount * 100 / purchasedPart.StartAmount) < 100)
                {
                    Image = @"Resources\Images\green.png";
                }
                else
                {
                    Image = @"Resources\Images\dark_green.png";
                }
                _products.Add(new DisplayObjectProduct
                {
                    Id = purchasedPart.Type + purchasedPart.Id,
                    Description = purchasedPart.Description,
                    Type = kad2SimSoft.Properties.Resources.dgWarehouseRowValuePurchased,
                    Amount = purchasedPart.Amount,
                    StartAmount = purchasedPart.StartAmount,
                    Ratio = purchasedPart.Amount * 100 / purchasedPart.StartAmount,
                    ImageSource = Image,
                    Price = purchasedPart.Price,
                    StockValue = purchasedPart.Amount * purchasedPart.Price
                });
            }
            Products = CollectionViewSource.GetDefaultView(_products);

            GroupedProducts = new ListCollectionView(_products);
            GroupedProducts.GroupDescriptions.Add(new PropertyGroupDescription(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderType));
        }

        public ObservableCollection<Point> PurchasedPartsDataPoints
        {
            get { return _purchasedPartsDataPoints; }
        }

        public ObservableCollection<Point> ManufacturedPartsDataPoints
        {
            get { return _manufacturedPartsDataPoints; }
        }

        public ObservableCollection<Point> EndproductsDataPoints
        {
            get { return _endproductsDataPoints; }
        }

        public List<PurchasedPart> PurchasedParts
        {
            get { return _purchasedParts; }
            set { _purchasedParts = value; }
        }

        public List<ManufacturedPart> ManufacturedParts
        {
            get { return _manufacturedParts; }
            set { _manufacturedParts = value; }
        }

        public List<Endproduct> Endproducts
        {
            get { return _endproducts; }
            set { _endproducts = value; }
        }

        public class Point : DependencyObject
        {
            public static readonly DependencyProperty _date = DependencyProperty.Register("Name", typeof(String), typeof(Point));
            public Point(String date, double value)
            {
                Date = date;
                Value = value;
            }
            public String Date
            {
                get { return (String)GetValue(_date); }
                set { SetValue(_date, value); }
            }
            public static readonly DependencyProperty _value = DependencyProperty.Register("Value", typeof(double), typeof(Point));
            public double Value
            {
                get { return (double)GetValue(_value); }
                set { SetValue(_value, value); }
            }
        }

        private void calculateColumnChart()
        {
            purchasedPartsColumnChartAxis.Maximum = 0;
            manufacturedPartsColumnChartAxis.Maximum = 0;
            endproductsColumnChartAxis.Maximum = 0;

            purchasedPartsColumnChartAxis.Maximum = purchasedPartsMax * 1.1;
            manufacturedPartsColumnChartAxis.Maximum = manufacturedPartsMax * 1.1;
            endproductsColumnChartAxis.Maximum = endproductsMax * 1.1;
        }

        private void fillOverviewSection()
        {
            //lb_Overview_PurchasedParts.Text = String.Format("{0:0,0.00}", purchasedPartsSummary) + " €";
            //lb_Overview_ManufacturedParts.Text = String.Format("{0:0,0.00}", manufcaturedPartsSummary) + " €";
            //lb_Overview_Endproducts.Text = String.Format("{0:0,0.00}", endproductsSummary) + " €";
            //lb_Overview_Overall.Text = String.Format("{0:0,0.00}", warehouseValue) + " €";
        }

        private class DisplayObjectProduct
        {
            public string Id { get; set; }
            public string Description { get; set; }
            public string Type { get; set; }
            public int Amount { get; set; }
            public int StartAmount { get; set; }
            public double Ratio { get; set; }
            public string ImageSource { get; set; }
            public double Price { get; set; }
            public double StockValue { get; set; }
        }

        private void RadioButton_DisplayItems_Checked(object sender, RoutedEventArgs e)
        {
            var button = sender as RadioButton;
            if (button.Content.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderPrice))
            {
                purchasedPartsColumnChartAxis.Title = "€";
                manufacturedPartsColumnChartAxis.Title = "€";
                endproductsColumnChartAxis.Title = "€";
                collectColumnDataPoints(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderPrice);
                chartHeader = kad2SimSoft.Properties.Resources.dgWarehouseColHeaderPrice;
            }
            else if (button.Content.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderAmount))
            {
                purchasedPartsColumnChartAxis.Title = kad2SimSoft.Properties.Resources.lbAmount;
                manufacturedPartsColumnChartAxis.Title = kad2SimSoft.Properties.Resources.lbAmount;
                endproductsColumnChartAxis.Title = kad2SimSoft.Properties.Resources.lbAmount;
                collectColumnDataPoints(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderAmount);
                chartHeader = kad2SimSoft.Properties.Resources.dgWarehouseColHeaderAmount;
            }
            else if (button.Content.Equals(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderStockValue))
            {
                purchasedPartsColumnChartAxis.Title = "€";
                manufacturedPartsColumnChartAxis.Title = "€";
                endproductsColumnChartAxis.Title = "€";
                collectColumnDataPoints(kad2SimSoft.Properties.Resources.dgWarehouseColHeaderStockValue);
                chartHeader = kad2SimSoft.Properties.Resources.dgWarehouseColHeaderStockValue;
            }

            calculateColumnChart();
            DataContext = this;
        }
    }
}
